package com.yb.web.statement.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.statement.StatementService;
import com.yb.bussiness.statement.StatementServiceImpl;
import com.yb.domain.statement.Statement;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.MemoryData;

/**
 * 公告相关Action
 * 
 * @author SFF
 * 
 */
public class StatementAction extends BaseAction {
    
    private static final long serialVersionUID = 688970037309060648L;
    
    private Statement statement;
    
    private StatementService service;
    
    private int total;
    
    private List rows;
    
    private String flag;
    
    public StatementAction() {
        service = new StatementServiceImpl();
    }
    
    public String queryStatementManage() {
        try {
            String page = RequestUtil.getParam(request, "page", "1");
            String row = RequestUtil.getParam(request, "rows", "10");
            Map<String, Object> map = service.findAll(page, row);
            rows = (List<Statement>)map.get("list");
            total = Integer.parseInt(map.get("total").toString());
        }
        catch (Exception e) {
            log.error("声明管理信息出错", e);
        }
        return Contants.JSONSTRING_RESULT_NAME;
    }
    
    public String saveOrUpdate() {
        try {
            service.saveOrUpdate(statement);
        }
        catch (Exception e) {
            log.error("保存或修改信息出错", e);
        }
        return "statementManage";
    }
    
    /*
     * 跳转信息页面
     */
    public String showStatementinfo() {
        try {
            Long id = RequestUtil.getParam(request, "id", 0l);
            statement = service.read(id);
            ActionContext context = ActionContext.getContext();
            context.put("statementList", MemoryData.getStatementList());
        }
        catch (Exception e) {
            log.error("查询信息出错", e);
        }
        return "showStatementinfo";
    }
    
    /*
     * 跳转信息页面
     */
    public String toStatementInfo() {
        try {
            Long id = RequestUtil.getParam(request, "id", 0l);
            statement = service.read(id);
        }
        catch (Exception e) {
            log.error("查询信息出错", e);
        }
        return "toStatementInfo";
    }
    
    /*
     * 跳转信息页面
     */
    public String toStatementInfoadmin() {
        try {
            statement = service.read(flag);
        }
        catch (Exception e) {
            log.error("查询信息出错", e);
        }
        return "toStatementInfoadmin";
    }
    
    public String modifyWeights() {
        try {
            Long id = RequestUtil.getParam(request, "id", 0l);
            int weights = RequestUtil.getParam(request, "weights", 0);
            service.modifyGoodsWeight(id, weights);
            response.getWriter().print("1");
        }
        catch (Exception e) {
            log.error("修改声明权重失败", e);
            try {
                response.getWriter().print("2");
            }
            catch (IOException e1) {
                log.error("修改声明权重失败", e1);
            }
        }
        return null;
    }
    
    /*
     * 跳转信息页面
     */
    public String toStatement() {
        try {
            statement = service.read(flag);
        }
        catch (Exception e) {
            log.error("查询信息出错", e);
        }
        if (flag.equals("8") || flag.equals("7")) {
            return "toAgreement";
        }
        return "toStatement";
    }
    
    public String delete() {
        String id = RequestUtil.getParam(request, "id", "");
        boolean isFlag = false;
        isFlag = service.delete(id);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        if (isFlag)
            message = "删除成功";
        else
            message = "删除失败";
        StringBuffer sb = new StringBuffer("[");
        sb.append("{\"message\":\"" + message + "\"" + "}");
        sb.append("]");
        
        try {
            out = response.getWriter();
            out.println(sb.toString());
        }
        catch (IOException e) {
            log.error("删除信息出错", e);
        }
        return null;
    }
    
    public Statement getStatement() {
        return statement;
    }
    
    public void setStatement(Statement statement) {
        this.statement = statement;
    }
    
    public String getFlag() {
        return flag;
    }
    
    public void setFlag(String flag) {
        this.flag = flag;
    }
    
    public int getTotal() {
        return total;
    }
    
    public void setTotal(int total) {
        this.total = total;
    }
    
    public List getRows() {
        return rows;
    }
    
    public void setRows(List rows) {
        this.rows = rows;
    }
    
}
